package cn.edu.fzu.homemaking.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.domain.entity.SysLabel;
import cn.edu.fzu.homemaking.domain.enums.LabelStatusEnum;
import cn.edu.fzu.homemaking.mapper.SysLabelMapper;
import cn.edu.fzu.homemaking.service.SysLabelService;
import cn.edu.fzu.homemaking.web.Result;

/**
 * @author yifan
 * @date 2023/4/5
 */
@Service
public class SysLabelServiceImpl implements SysLabelService {

    @Resource
    SysLabelMapper sysLabelMapper;

    @Override
    public Result listLabels(String name, LabelStatusEnum status, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            try (Page<SysLabel> page = PageHelper.startPage(pageNum, pageSize)) {
                sysLabelMapper.list(name, status);
                return new Result(new PageVO<>(page.getTotal(), page.getResult()));
            }
        }
        return new Result(sysLabelMapper.list(name, status));
    }


    @Override
    public void addLabels(List<String> labelNames) {
        sysLabelMapper.add(labelNames, LabelStatusEnum.NORMAL);
    }


    @Override
    public void updateLabel(Long id, String name, LabelStatusEnum status) {
        sysLabelMapper.update(id, name, status);
    }


    @Override
    public Result getByCompanyId(Long companyId, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            try (Page<SysLabel> page = PageHelper.startPage(pageNum, pageSize)) {
                sysLabelMapper.getByCompanyId(companyId);
                return new Result(new PageVO<>(page.getTotal(), page.getResult()));
            }
        }
        return new Result(sysLabelMapper.getByCompanyId(companyId));
    }
}
